from django.core.paginator import Paginator
from django.db.models import F
from django.shortcuts import render, get_object_or_404

# Create your views here.
from system.models import News
from utils import constants
from utils.common import VerifyCode


def news_list(request, tempalte_name='news_list.html'):
    """新闻列表"""
    # 获取当前页码
    page = request.GET.get('page', 1)
    page_size = 20  # 每页的数量
    news = News.objects.filter(
        types=constants.NEWS_TYPE_NEW,
        is_valid=True
    )
    # 获取分页器
    paginator = Paginator(news, page_size)
    page_data = paginator.page(page)
    return render(request, tempalte_name, {
        'page_data': page_data
    })

def news_detail(request, pk, template_name='news_info.html'):
    """新闻详情"""
    new_obj = get_object_or_404(News, pk=pk, is_valid=True)
    # 每查看一次，浏览次数 + 1（F()函数从数据库操作层面修改数据，避免同时操作时竞态条件）
    new_obj.view_count = F('view_count') + 1
    new_obj.save()
    # 重新从数据库取数据
    new_obj.refresh_from_db()
    return render(request, template_name, {
        'new_obj': new_obj
    })

def verify_code(request):
    """验证码的显示"""
    client = VerifyCode(request)
    return client.gen_code()
